﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    public class AccountMonthAnalysis
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }

        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }





        ///// <summary>
        ///// 删除当月所有记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthAnalysis where t.Month == month && t.Year == year select t;
        //    foreach (Data.Month.AccountMonthAnalysis c in query)
        //    {
        //        db.AccountMonthAnalysis.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 保存账号月变化情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitMonthAccountAnalysis(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        AccountMonthAnalysis change = new AccountMonthAnalysis();
        //        change.AccountId = account.Id;
        //        var _query = from t in db.AccountDayAnalysis where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id select t;
        //        var query2 = from t in db.AccountMonthAnalysis where t.Month == month && t.Year == year && t.AccountId == account.Id select t;
        //        if (query2.Count() > 0)//有记录更新
        //        {
        //            change = query2.First();
        //        }
        //        if (_query.Count() > 0)
        //        {
        //            change.AvgBillBenefit = _query.Sum(a => a.AvgBillBenefit);
        //            change.AvgPostionTime = _query.Sum(a => a.AvgPostionTime);
        //            change.MaxReturn = _query.Max(a => a.MaxReturn);
        //            change.OverStopLoss = _query.Sum(a => a.OverStopLoss);
        //            change.PingJinPing = _query.Sum(a => a.PingJinPing);
        //            change.RaiseRate = _query.Sum(a => a.RaiseRate);
        //            change.RiskCount = _query.Sum(a => a.RiskCount);
        //            change.Utilization = _query.Sum(a => a.Utilization);
        //            change.WinRate = _query.Sum(a => a.WinRate);
        //            change.Yield = _query.Sum(a => a.Yield);

        //        }
        //        change.Year = year;
        //        change.Month = month;
        //        if (query2.Count() == 0)
        //        {
        //            db.AccountMonthAnalysis.Add(change);
        //        }
        //    }
        //    db.SaveChanges();
        //}
    }
}